package net.dgg.dggcloud.service.jrcrm.service.orf;

import net.dgg.dggcloud.core.json.JsonUtil;
import net.dgg.dggcloud.core.util.StringUtils;
import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.common.exception.BizException;
import net.dgg.dggcloud.service.jrcrm.dao.orf.OrderBusinessDao;
import net.dgg.dggcloud.service.jrcrm.entity.orf.OrderBusiness;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.jrcrm.service.cms.BillNumberRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 鲜林材
 *         订单：子订单表
 * @create 2017-11-06 16:11
 **/
@Service
public class OrderBusinessService extends BaseService {

    @Autowired
    private OrderBusinessDao orderBusinessDao;

    @Resource
    private BillNumberRuleService bs;

    /**
     * 保存子订单信息
     *
     * @param orderBusiness
     */
    @Transactional
    public void saveOrderBusiness(OrderBusiness orderBusiness) {
        try {
            long id = this.keyWorker.nextId();
            orderBusiness.setId(id);
            this.repository.save(orderBusiness);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("保存子订单异常:" + e.getMessage(), 10006);
        }

    }


    /**
     * 通过map条件查询订单子订单信息
     *
     * @param map 查询条件<br/>
     *            ordeId 订单id<br/>
     * @return
     */
    public List<Map> queryOrderBusinessByMap(Map map) {
        return orderBusinessDao.queryOrderBusinessByMap(map);
    }


    /**
     * 通过实体对应的属性，查询对应信息
     *
     * @param properName:实现属性
     * @param value：对应值
     * @return
     */
    public List<OrderBusiness> queryOrder(String properName, Object value) {
        return this.repository.findByProperty(OrderBusiness.class, properName, value);
    }

    /**
     * 批量保存子订单信息
     *
     * @param map 参数传输  <br/>
     *            businesInfos 业务信息  <br/>
     *            user 用户对象 <br/>
     *            orderId   订单id<br/>
     *            status 订单状态  <br/>
     */
    @Transactional
    public String saveOrUpdateBatchOrderBusiness(Map map) {
        String result = null;
        try {
            List<OrderBusiness> businessMaps = new ArrayList();
            String businesInfos = map.get("businesInfos") == null ? "" : map.get("businesInfos").toString();
            String orderNo = map.get("orderNo") == null ? "" : map.get("orderNo").toString();
            if (StringUtils.isNotEmpty(businesInfos)) {
                List<OrderBusiness> orderBusinessess = JsonUtil.json2List(businesInfos, OrderBusiness.class);
                Long orderId = (Long) map.get("orderId");
                User user = (User) map.get("user");
                if (!orderBusinessess.isEmpty()) {
                    int i = 0;
                    for (OrderBusiness obj : orderBusinessess) {
                        i++;
                        obj.setOrderId(orderId);
                        obj.setStatus(map.get("status").toString());
                        if (obj.getId() != null) {
                            OrderBusiness oldOrderBusiness = queryOrderBusinessById(obj.getId());
                            if (oldOrderBusiness != null) {
                                oldOrderBusiness.setStatus(obj.getStatus());
                                oldOrderBusiness.setBtName(obj.getBtName());
                                oldOrderBusiness.setBtIdentifying(obj.getBtIdentifying());
                                oldOrderBusiness.setTypeIdentifying(obj.getTypeIdentifying());
                                oldOrderBusiness.setProductId(obj.getProductId());
                                oldOrderBusiness.setServiceCharge(obj.getServiceCharge());
                                oldOrderBusiness.setRateType(obj.getRateType());
                                oldOrderBusiness.setLoansAmount(obj.getLoansAmount());
                                oldOrderBusiness.setFlowUserId(obj.getFlowUserId());
                                oldOrderBusiness.setOrganizationId(obj.getOrganizationId());
                                oldOrderBusiness.setContractNo(obj.getContractNo());
                                oldOrderBusiness.setChannelId(obj.getChannelId());
                                oldOrderBusiness.setUpdaterUser(user);
                            }
                            this.updateOrderBusiness(oldOrderBusiness);
                        } else {
                            obj.setOrderBusinessNo(orderNo + "-" + i);//订单编号
                            obj.setCreateUser(user);
                            this.saveOrderBusiness(obj);
                        }
                        businessMaps.add(obj);
                    }
                }
                result = JsonUtil.obj2Json(businessMaps);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("子订单批量处理异常:" + e.getMessage(), 10006);
        }
        return result;
    }


    /**
     * 更新子订单信息
     *
     * @param orderBusiness
     */
    @Transactional
    public void updateOrderBusiness(OrderBusiness orderBusiness) {
        try {
            this.repository.saveOrUpdate(orderBusiness);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("更新子订单异常" + e.getMessage(), 10006);
        }

    }

    /**
     * 通过主键id查询订单业务信息
     *
     * @param id
     * @return
     */
    public OrderBusiness queryOrderBusinessById(Long id) {
        return this.repository.get(OrderBusiness.class, id);
    }

    /**
     * 通过订单id查询订单业务
     *
     * @param orderId
     * @return
     */
    public List<OrderBusiness> queryOrderBusinessByOrderId(long orderId) {
        return this.repository.findByProperty(OrderBusiness.class, "orderId", orderId);
    }

    /**
     * 通过订单id删除订单业务信息
     *
     * @param parameterMap 传入orderId
     */
    @Transactional
    public void deleteOrderBusinessByMap(Map parameterMap) {
        orderBusinessDao.deleteOrderBusinessByMap(parameterMap);
    }


    public String queryOrderBusinessByContractNo(Map map){
        List<Map> orderList = orderBusinessDao.queryOrderBusinessByContractNo(map);
        String orderIds = "";
        for(Map m:orderList){
            orderIds = orderIds + m.get("orderid") +",";
        }
        if(orderIds.length()>0){
            orderIds = orderIds.substring(0,orderIds.length()-1);
        }
        return orderIds;
    }
}
